package com.example.aicodehelper.ai;

import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import dev.langchain4j.model.chat.ChatModel;

@Configuration
public class AiCodeHelperServiceFactory {
    @Resource
    private ChatModel myQwenChatModel;
    @Resource
    private ChatMemoryStore chatMemoryStore; // 注入你自定义的 PersistentChatMemoryStore

    // 构建带持久化记忆的 ChatMemory
    @Bean(name = "daoChatMemory")
    public ChatMemory chatMemory() {
        ChatMemory chatMemory=MessageWindowChatMemory.builder()
                .id("user123") // 注意：实际需动态绑定用户ID（如从登录信息获取）
                .maxMessages(10)
                .chatMemoryStore(chatMemoryStore)
                .build();
        return chatMemory;
    }

    @Bean
    public AiCodeHelperService aiCodeHelperService(@Qualifier("daoChatMemory") ChatMemory chatMemory){  //spring自动注入bean
        AiCodeHelperService aiCodeHelperService=AiServices.builder(AiCodeHelperService.class)
                .chatModel(myQwenChatModel)
                .chatMemory(chatMemory)
                .build();
        return aiCodeHelperService;
    }

    /*@Bean
    public AiCodeHelperService aiCodeHelperService(){
        ChatMemory chatMemory= MessageWindowChatMemory.withMaxMessages(10);     //10条会话记忆
        AiCodeHelperService aiCodeHelperService=AiServices.builder(AiCodeHelperService.class)
                .chatModel(myQwenChatModel)
                .chatMemory(chatMemory)
                .build();
        return aiCodeHelperService;
        //return AiServices.create(AiCodeHelperService.class,myQwenChatModel);    //动态代理
    }*/
    /*
    create 是 “快捷方式”，自动默认配置
    builder 构造器是 “定制化工具”，适合需要灵活配置（如自定义会话记忆、集成工具）的场景。
    两者最终都会生成接口的动态代理对象
    */
}
